package com.easylinkin.linkappapi.hcmy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.hcmy.entity.AquacultureSpace;
import com.easylinkin.linkappapi.hcmy.service.AquacultureService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * class info :水产养殖
 *
 * @author liuqihang
 * @date 2021/10/18 15:09
 */
@RestController
@RequestMapping("/hcmy/aquaculture")
public class AquacultureController {

    @Resource
    private AquacultureService aquacultureService;

    @PostMapping("savaAquaculture")
    public RestMessage savaAquaculture(@RequestBody @Valid AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace, "水产养殖不能为空");
        String result = aquacultureService.savaAquaculture(aquacultureSpace);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("updateAquaculture")
    public RestMessage updateAquaculture(@RequestBody @Valid AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace, "水产养殖不能为空");
        String result = aquacultureService.updateAquaculture(aquacultureSpace);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("getAquaculturePage")
    public RestMessage getFarmersPage(@RequestBody RequestModel<AquacultureSpace> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        IPage<AquacultureSpace> livestockFarmersList = aquacultureService.getAquaculturePage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(livestockFarmersList).build();
    }

    @PostMapping("deleteAquaculture")
    public RestMessage deleteAquaculture(@RequestBody AquacultureSpace aquacultureSpace) {
        Assert.notNull(aquacultureSpace, "水产养殖不能为空");
        aquacultureService.deleteAquaculture(aquacultureSpace);
        return RestBuilders.successBuilder().message("success").build();
    }
}
